package com.example.goods.controller;

import com.example.goods.bean.ProductInfo;
import com.example.goods.common.ResponseCode;
import com.example.goods.common.ResultResponse;
import com.example.goods.service.ProductInfoService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/7/4
 */
@CrossOrigin
@RequestMapping("/product/")
@RestController
@AllArgsConstructor
public class ProductInfoController {
    private final ProductInfoService productInfoService;

    /**
     * 添加商品
     * @param productInfo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public ResultResponse<ProductInfo> addProduct(ProductInfo productInfo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultResponse.createByErrorCodeMessage(ResponseCode.FAILURE.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        return productInfoService.addProduct(productInfo);
    }

    /**
     * 查询商品列表按价格从低到高升序排序
     * @return
     */
    @GetMapping("list_ascending")
    public ResultResponse<List<ProductInfo>> listAscending(){
        return productInfoService.ascending();
    }

    /**
     * 查询商品列表按价格从高到低降序排序
     * @return
     */

    @GetMapping("list_descending")

    public ResultResponse<List<ProductInfo>> listDescending(){
        return productInfoService.descending();
    }
}
